package com.spring.oaassist.web.service.impl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.spera.aliyunContent.AliyunContent;
import com.spera.shopoa.util.EzvizUtils;
import com.spring.common.web.dao.CommonApiMapper;
import com.spring.oaassist.web.dao.EzvizMapper;
import com.spring.oaassist.web.service.EzvizService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.UDate;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.ImputStreamObj;
import com.spring.tools.SpUtil;

@Service
public class EzvizServiceImpl implements EzvizService{
	
	@Resource private EzvizMapper ezvizMapper;
	
	@Resource
	private CommonApiMapper commonApiMapper;

	@Override
	public List<Map<String, Object>> queryEzviz(Map<String, Object> parm) {
		
		//获取本地摄像头配置
		List<Map<String, Object>> retLocalData = ezvizMapper.queryEzviz(parm);
		//获取萤石摄像头列表
		List<Map<String, Object>> dataList = getEzvizDataist();
		if(dataList!=null){
			Map<String, Object> dataOne = Maps.newHashMap();
			for(int i=0;i<dataList.size();i++){
				dataOne = dataList.get(i);
				for(Map<String, Object> loDataOne:retLocalData){
					if(dataOne.get("cameraId").equals(loDataOne.get("camera_id"))){
						Map<String,String> parksel = Maps.newHashMap();
						Map<String,String> typesel = Maps.newHashMap();
						Map<String,String> prosel = Maps.newHashMap();
						dataList.get(i).put("ezvizId", loDataOne.get("id"));
						parksel.put("id", ApplicationUtils.Object2String(loDataOne.get("kidsPark")));
						parksel.put("name", ApplicationUtils.Object2String(loDataOne.get("kidsParkName")));
						dataList.get(i).put("kidsPark", parksel);
						typesel.put("code", ApplicationUtils.Object2String(loDataOne.get("ezvizType")));
						typesel.put("name", ApplicationUtils.Object2String(loDataOne.get("ezvizTypeName")));
						dataList.get(i).put("ezvizType", typesel);
						prosel.put("code", ApplicationUtils.Object2String(loDataOne.get("orderProductId")));
						prosel.put("name", ApplicationUtils.Object2String(loDataOne.get("productName")));
						dataList.get(i).put("product", prosel);
						dataList.get(i).put("ezvizName", ApplicationUtils.Object2String(loDataOne.get("ezvizName")));
					}
				}
			}
		}
		
		return dataList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public BaseResult saveEzviz(List<Map<String, Object>> dataForm) {
		BaseResult ret = new BaseResult("1","操作成功");
//		Map<String, Object> parm = Maps.newHashMap();
		if(dataForm!=null&&dataForm.size()>0){
			for(Map<String, Object> oneM:dataForm){
				if(ApplicationUtils.isNotNull(oneM.get("ezvizId"))
						&&ApplicationUtils.isNotNull(((Map<String, Object>)oneM.get("ezvizType")).get("code"))
						&&ApplicationUtils.isNotNull(oneM.get("ezvizName"))
						&&ApplicationUtils.isNotNull(((Map<String, Object>)oneM.get("kidsPark")).get("id"))){
					//update
					//转存图片
					String oss_id = fileTransOss(ApplicationUtils.Object2String(oneM.get("picUrl")));
					oneM.put("base_oss_fileID", oss_id);
					ezvizMapper.updateEzviz(oneM);
				}else if(ApplicationUtils.isNotNull(oneM.get("ezvizName"))
						&&ApplicationUtils.isNotNull(((Map<String, Object>)oneM.get("ezvizType")).get("code"))
						&&ApplicationUtils.isNotNull(((Map<String, Object>)oneM.get("kidsPark")).get("id"))){
					//insert
					oneM.put("ezvizId", ApplicationUtils.randomUUID());
					//转存图片
					String oss_id = fileTransOss(ApplicationUtils.Object2String(oneM.get("picUrl")));
					oneM.put("base_oss_fileID", oss_id);
					ezvizMapper.saveEzviz(oneM);
				}
			}
		}
	
		return ret;
	}
	
	@Override
	public BaseResult delEzviz(String id) {
		BaseResult ret = new BaseResult();
		int i = ezvizMapper.delEzviz(id);
		if(i==0){
			ret.setStatus("0").setMsg("操作失败");
		}
		return ret;
	}
	
	private String fileTransOss(String file_url){
		String oss_id="";
		if(StringUtils.isNotBlank(file_url)){
			try{
				ImputStreamObj imsObj = SpUtil.getHttpInputStream(file_url);
				oss_id = ApplicationUtils.randomUUID();
				String newfilename = "ezviz"+ApplicationUtils.randomUUID();
				String filePath = SpUtil.imageFileUpOss(AliyunContent.fileBucket,"ezviz_photo/"+UDate.getDate()+"/"+newfilename, imsObj.getIms(),imsObj.getSize());
				Map<String,Object> im = new HashMap<String,Object>();
				im.put("id", oss_id);
				im.put("bucket", AliyunContent.fileBucket);
				im.put("url", filePath);
				im.put("etag", "");
				im.put("key", "ezviz_photo/"+UDate.getDate()+"/"+newfilename);
				im.put("token", "");
				im.put("file_type", "img");
				commonApiMapper.addOssFile(im);
				imsObj.getIms().close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		return oss_id;
	}
	
	//根据萤石云接口获取设备列表数据
	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> getEzvizDataist(){
		Map<String, Object> accessToken = accessToken();
		Map<String, Object> ret = (Map<String, Object>)accessToken.get("result");
		System.out.println("------ret--->"+JSON.toJSONString(ret));
		System.out.println("------retCode--->"+ApplicationUtils.Object2String(ret.get("code")));
		if("200".equals(ApplicationUtils.Object2String(ret.get("code")))){
			ret = (Map<String, Object>)accessToken.get("result");
			Map<String, Object> data = (Map<String, Object>)ret.get("data");
			String accesstk = ApplicationUtils.Object2String(data.get("accessToken"));
			if(StringUtils.isNotBlank(accesstk)){
				Map<String, Object> retData = ezvizList(accesstk);
				ret = (Map<String, Object>)retData.get("result");
				List<Map<String, Object>> dataList = (List<Map<String, Object>>)ret.get("data");
				for(int i=0;i<dataList.size();i++){
					Map<String,String> parksel = Maps.newHashMap();
					Map<String,String> typesel = Maps.newHashMap();
					Map<String,String> prosel = Maps.newHashMap();
					parksel.put("id", "");
					parksel.put("name", "");
					dataList.get(i).put("kidsPark", parksel);
					typesel.put("code", "");
					typesel.put("name","");
					dataList.get(i).put("ezvizType", typesel);
					prosel.put("code", "");
					prosel.put("name","");
					dataList.get(i).put("product", prosel);
				}
				return dataList;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	//获取accesstoken
	private Map<String, Object> accessToken() {
		Map<String, Object> paramsMap = Maps.newHashMap();
		paramsMap.put("phone", "18094577155");
		Map<String, Object> ret = EzvizUtils.paramsInit("token/getAccessToken", paramsMap);
		
		Map<String, Object> accessToken = EzvizUtils.doPost(ret);
		System.out.println("------accessToken---->"+accessToken);
		return accessToken;
	}

	//获取设备列表
	private Map<String, Object> ezvizList(String accessToken) {
		Map<String, Object> paramsMap = Maps.newHashMap();
		paramsMap.put("accessToken", accessToken);
		paramsMap.put("pageSize", 999);
		paramsMap.put("pageStart", 0);
		Map<String, Object> ret = EzvizUtils.paramsInit("cameraList", paramsMap);
		
		Map<String, Object> retM = EzvizUtils.doPost(ret);
		return retM;
	}

}
